public class MyLinkedList {

    private Node head;
    private Node last;
    private int size = 0;

    static class Node {
        private int val;
        private Node prev;
        private Node next;
        private Node(){}
        private Node(int data) {
            this.val = data;
        }
    }


    //头插法
    public void addFirst(int data) {
        Node node = new Node(data);
        if(head == null) {
            head = node;
            last = node;
            size++;
            return;
        }

        node.next = head;
        head.prev = node;
        head = node;
        size++;
    }


    //尾插法
    public void addLast(int data) {
        Node node = new Node(data);
        if(last == null) {
            head = node;
            last = node;
            size++;
            return;
        }

        last.next = node;
        node.prev = last;
        last = node;
        size++;
    }


    //任意位置插入,第一个数据节点为0号下标
    public boolean addIndex(int index,int data) {
        if(index < 0 || index > size) {
            return false;
        }

        if(index == 0) {
            addFirst(data);
            return true;
        }
        if(index == size) {
            addLast(data);
            return true;
        }
        Node cur = head;
        while(index > 0) {
            cur = cur.next;
            index--;
        }
        Node node = new Node(data);
        node.next = cur;
        node.prev = cur.prev;
        cur.prev.next = node;
        cur.prev = node;
        size++;
        return  true;
    }


    //查找是否包含关键字key是否在双向链表当中
    public boolean contains(int key) {
        if(head == null) return false;

        Node cur = head;
        while(cur != null) {
            if(cur.val == key) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }


    //删除第一次出现关键字为key的节点
    public void remove(int key) {
        if(head == null) return;

        if(head.val == key) {
            head = head.next;
            size--;
            return;
        }

        Node cur = head.next;
        while(cur != null) {
            if(cur.val == key) {
                cur.prev.next = cur.next;
                cur.next.prev = cur.prev;
                size--;
                return;
            }
            cur = cur.next;
        }
    }


    //删除所有值为key的节点
    public void removeAllKey(int key) {
        if(head == null) return;

        Node cur = head.next;
        while(cur != null) {
            if(cur.val == key) {
                cur.prev.next = cur.next;
                cur.next.prev = cur.prev;
                size--;
            }
            cur = cur.next;
        }
        if(head.val == key) {
            head = head.next;
            size--;
        }
    }


    //得到单链表的长度
    public int size() {
        return size;
    }


    public void display() {
        Node cur = head;
        while (cur != null) {
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }


    public void clear() {
        head = last = null;
    }
}
